﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Paxman.Misc;

namespace Paxman.Objects
{
    class Player:Sprite
    {
        struct AutomaticMoviment
        {
            public bool active;
            public Vector2 positionChange;
            public Direction newDirection;
        }

        Animation walkingUpAnim;
        Animation walkingDownAnim;
        Animation walkingLeftAnim;
        Animation walkingRightAnim;

        Direction currentDirection;

        AutomaticMoviment autoMoviment;

        Map map;

        public Player(Map map)
        {
            vel = 3;
            SetOffset(Util.borderLeftGameArea, Util.borderTopGameArea);
            this.map = map;

            walkingRightAnim = new Animation("WalkingRight", 28, 28, 0, 5, 20);
            walkingLeftAnim = new Animation("WalkingLeft", 28, 28, 28, 5, 20);
            walkingUpAnim = new Animation("WalkingUp", 28, 28, 56, 5, 20);
            walkingDownAnim = new Animation("WalkingDown", 28, 28, 84, 5, 20);

            currentDirection = Direction.Stopped;
            currentAnimation = walkingRightAnim;
            currentAnimation.GoToAndStop(0);

            autoMoviment = new AutomaticMoviment();
            autoMoviment.active = false;
        }

        public void SetInitialPosition()
        {
            Center = new Vector2(252, 423);
        }

        public void Update()
        {
            Point playertilePos = map.TransformInGridPosition(Center);

            if (Controls.IsKeyDown(Keys.Up) && currentDirection != Direction.Up)
            {
                playertilePos.Y--;
                if (map.GetTileType(playertilePos) != TileType.Wall)
                {
                    currentDirection = Direction.Up;
                    currentAnimation = walkingUpAnim;
                    currentAnimation.PlayFromInit();
                    Center = map.GetCenterTileInPosition(Center);
                }
            }
            else if (Controls.IsKeyDown(Keys.Down) && currentDirection != Direction.Down)
            {
                playertilePos.Y++;
                if (map.GetTileType(playertilePos) != TileType.Wall)
                {
                    currentDirection = Direction.Down;
                    currentAnimation = walkingDownAnim;
                    currentAnimation.PlayFromInit();
                    Center = map.GetCenterTileInPosition(Center);
                }
            }
            else if (Controls.IsKeyDown(Keys.Left) && currentDirection != Direction.Left)
            {
                playertilePos.X--;
                if (map.GetTileType(playertilePos) != TileType.Wall)
                {
                    currentDirection = Direction.Left;
                    currentAnimation = walkingLeftAnim;
                    currentAnimation.PlayFromInit();
                    Center = map.GetCenterTileInPosition(Center);
                }
            }
            else if (Controls.IsKeyDown(Keys.Right) && currentDirection != Direction.Right)
            {
                playertilePos.X++;
                if (map.GetTileType(playertilePos) != TileType.Wall)
                {
                    currentDirection = Direction.Right;
                    currentAnimation = walkingRightAnim;
                    currentAnimation.PlayFromInit();
                    Center = map.GetCenterTileInPosition(Center);
                }
            }

            switch (currentDirection)
            {
                case Direction.Up:
                    position.Y -= vel;
                    break;
                case Direction.Down:
                    position.Y += vel;
                    break;
                case Direction.Left:
                    position.X -= vel;
                    break;
                case Direction.Right:
                    position.X += vel;
                    break;
            }

            if (currentDirection != Direction.Stopped && !currentAnimation.IsPlaying())
                currentAnimation.Play();

            CheckMapCollision();

            TileType tileType = map.GetTileType(Center);
            if (tileType == TileType.Dot || tileType == TileType.Energizer)
            {
                map.totalDots--;
                map.ClearTile(Center);
            }
        }

        private void CheckForAutomaticMoviment(Direction newDirection)
        {
            Point playertilePos = map.TransformInGridPosition(Center);

        }

        private void CheckMapCollision()
        {
            Point playertilePos = map.TransformInGridPosition(Center);
            Vector2 centerCurrentTile = map.GetCenterTileInPosition(Center);
            bool collided = false;

            switch (currentDirection)
            {
                case Direction.Up:
                    playertilePos.Y--;
                    if (map.GetTileType(playertilePos) == TileType.Wall && (Center.Y < centerCurrentTile.Y))
                        collided = true;
                    break;
                case Direction.Down:
                    playertilePos.Y++;
                    if (map.GetTileType(playertilePos) == TileType.Wall && (Center.Y > centerCurrentTile.Y))
                        collided = true;
                    break;
                case Direction.Left:
                    playertilePos.X--;
                    if (map.GetTileType(playertilePos) == TileType.Wall && (Center.X < centerCurrentTile.X))
                        collided = true;
                    break;
                case Direction.Right:
                    playertilePos.X++;
                    if (map.GetTileType(playertilePos) == TileType.Wall && (Center.X > centerCurrentTile.X))
                        collided = true;
                    break;
            }

            if (collided)
            {
                Center = centerCurrentTile;
                currentDirection = Direction.Stopped;
                currentAnimation.Pause();
            }
        }

        public bool IsCurrentDirectionHorizontal()
        {
            if (currentDirection == Direction.Left || currentDirection == Direction.Right)
                return true;
            else
                return false;
        }

        public bool IsCurrentDirectionVertical()
        {
            if (currentDirection == Direction.Up || currentDirection == Direction.Down)
                return true;
            else
                return false;
        }
    }
}
